/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with 
 * this distribution in the License.txt file.
 */

package jdbcmanager.sqlverboser;

import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import jdbcmanager.swing.table.*;
import jdbcmanager.dbtools.*;

/**
 * A table showing executed SQL commands and information about them.
 *  
 * @author Francisco Morero Peyrona
 */
final class TablePane extends JScrollPane
{
   private JxTableVector table;

   TablePane()
   {
      getViewport().setBackground( Color.white );
      
      Vector vHead = new Vector();
             vHead.add( "Catalog"     );
             vHead.add( "SQL Command" );
             vHead.add( "Exec Time"   );

      this.table = new JxTableVector( new Vector(), vHead );          // By default table is read-only
      this.table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
      this.table.setShowVerticalLines( false );
      this.table.getColumnModel().getColumn( 0 ).setMaxWidth( 110 );
      this.table.getColumnModel().getColumn( 2 ).setMaxWidth( 70 );
      
      this.table.addMouseListener( new MouseAdapter()
      {
         public void mouseClicked( MouseEvent evt )
         {
            if( evt.getClickCount() == 2 && SwingUtilities.isLeftMouseButton( evt ) )
               inspectSelectedRow();
         }
      } );

      setViewportView( this.table );
   }

   void add( String sCatalog, String sSQL, long nTime )
   {
      Vector vRow = new Vector( 3 );
             vRow.add( sCatalog );
             vRow.add( sSQL.trim() );
             vRow.add( nTime + "ms" );

      this.table.addRow( vRow );
      
      int nRows = this.table.getRowCount() - 1;

      this.table.setRowSelectionInterval( nRows, nRows );                            // Highlights last row
      getViewport().scrollRectToVisible( this.table.getCellRect( nRows, 0, true ) ); // Scrolls to last row
   }

   void clearTable()
   {
      this.table.setData( (Vector) null );
   }
   
   int getRowCount()
   {
      return this.table.getRowCount();
   }

   void inspectSelectedRow()
   {
      int nRow = table.getSelectedRow();
      
      if( nRow > -1 )
      {
         Vector vRow = (Vector) this.table.getData().get( nRow );
         String sCat = (String) vRow.get( 0 );
         String sSQL = (String) vRow.get( 1 );
         String sMS  = (String) vRow.get( 2 );
   
         (new JSQLViewer( "Command executed in " + sMS + " on '" + sCat + "'", sCat, sSQL )).setVisible( true );
      }
  }
   
   /**
    * Changes first column header title to reflect preferred DBMS term for Catalog.
    * <p>
    * This is just a cosmetic method.
    */
   void updateCatalogTerm()
   {
      this.table.getTableHeader().getColumnModel().getColumn( 0 ).setHeaderValue( DbUtil.getAgentDBMS().getAgentMetaData().getCatalogTerm() );   
      this.table.getTableHeader().repaint();   // Needed
   }
}